package com.jfs.envent.listener;

import cn.hutool.json.JSONUtil;
import com.jfs.envent.entity.MessageConfigDownload;
import com.jfs.health.HealthCheckReactor;
import com.jfs.health.HealthCheckTask;
import com.jfs.properties.BeatExecutorProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Description 设备配置信息监听器
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/8/1
 */
@Component
@Slf4j
@AllArgsConstructor
public class DeviceConfigDownloadEventListener implements ApplicationListener<MessageConfigDownload> {
    private final HealthCheckReactor healthCheckReactor;
    private final BeatExecutorProperties beatExecutorProperties;
    @Async
    @Override
    public void onApplicationEvent(MessageConfigDownload event) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("【设备号:{},设备ip:{},设备端口号:{}】，查询到设备配置信息：{}", event.getDeviceID(), event.getIp(), event.getPort(), JSONUtil.toJsonStr(event));
            }
            //启动心跳检测
            healthCheckReactor.scheduleCheck(new HealthCheckTask(event.getDeviceID(), event.getBasicParam().getHeartBeatInterval() + 5),
                    5000, beatExecutorProperties.getHeartBeatInterval());
        } catch (Exception e) {
            log.error("设备配置信息查询监听器发生异常:{}", e.getMessage());
        }

    }
}
